package com.sanshi.project.assets.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.entity.PrintTemplateEntity;
import com.sanshi.project.assets.service.PrintTemplateService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 打印模版配置表
 *
 * @author nikon
 * @date 2023-11-16 16:22:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/printTemplate")
@Tag(description = "printTemplate", name = "打印模版配置表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PrintTemplateController {

    private final PrintTemplateService printTemplateService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param printTemplate 打印模版配置表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_view')")
    public R getPrintTemplatePage(@ParameterObject Page page, @ParameterObject PrintTemplateEntity printTemplate) {
        LambdaQueryWrapper<PrintTemplateEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(printTemplate.getName()), PrintTemplateEntity::getName, printTemplate.getName());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getType()), PrintTemplateEntity::getType, printTemplate.getType());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getDuplex()), PrintTemplateEntity::getDuplex, printTemplate.getDuplex());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getStatus()), PrintTemplateEntity::getStatus, printTemplate.getStatus());
        wrapper.orderByDesc(PrintTemplateEntity::getUpdateTime);
        return R.ok(printTemplateService.page(page, wrapper));
    }

    /**
     * 通过id查询打印模版配置表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(printTemplateService.getByIdDeep(id));
    }

    /**
     * 新增打印模版配置表
     *
     * @param printTemplate 打印模版配置表
     * @return R
     */
    @Operation(summary = "新增打印模版配置表", description = "新增打印模版配置表")
    @SysLog("新增打印模版配置表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_add')")
    public R save(@RequestBody PrintTemplateEntity printTemplate) {
        return R.ok(printTemplateService.saveDeep(printTemplate));
    }

    /**
     * 修改打印模版配置表
     *
     * @param printTemplate 打印模版配置表
     * @return R
     */
    @Operation(summary = "修改打印模版配置表", description = "修改打印模版配置表")
    @SysLog("修改打印模版配置表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_edit')")
    public R updateById(@RequestBody PrintTemplateEntity printTemplate) {
        return R.ok(printTemplateService.updateDeep(printTemplate));
    }

    /**
     * 通过id删除打印模版配置表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除打印模版配置表", description = "通过id删除打印模版配置表")
    @SysLog("通过id删除打印模版配置表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(printTemplateService.removeDeep(ids));
    }

    /**
     * 通过id删除打印模版配置表子表数据
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除打印模版配置表子表数据", description = "通过id删除打印模版配置表子表数据")
    @SysLog("通过id删除打印模版配置表子表数据")
    @DeleteMapping("/child")
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_del')")
    public R removeChild(@RequestBody Long[] ids) {
        return R.ok(printTemplateService.removeChild(ids));
    }

    /**
     * 导出excel 表格
     *
     * @param printTemplate 查询条件
     * @param ids           导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_export')")
    public List<PrintTemplateEntity> export(PrintTemplateEntity printTemplate, Long[] ids) {
        return printTemplateService.list(Wrappers.lambdaQuery(printTemplate).in(ArrayUtil.isNotEmpty(ids), PrintTemplateEntity::getId, ids));
    }

    /**
     * 打印模版配置表全量查询
     *
     * @param printTemplate 打印模版配置表
     * @return
     */
    @Operation(summary = "打印模版配置表查询", description = "打印模版配置表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('assets_printTemplate_view')")
    public R getLesseeList(@ParameterObject PrintTemplateEntity printTemplate) {
        LambdaQueryWrapper<PrintTemplateEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(printTemplate.getName()), PrintTemplateEntity::getName, printTemplate.getName());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getType()), PrintTemplateEntity::getType, printTemplate.getType());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getDuplex()), PrintTemplateEntity::getDuplex, printTemplate.getDuplex());
        wrapper.eq(StrUtil.isNotBlank(printTemplate.getStatus()), PrintTemplateEntity::getStatus, printTemplate.getStatus());
        wrapper.orderByDesc(PrintTemplateEntity::getUpdateTime);
        wrapper.orderByDesc(PrintTemplateEntity::getUpdateTime);
        return R.ok(printTemplateService.list(wrapper));
    }
}